package leetCode.sample;

import org.junit.Test;

public class SumNumbersSolution {

    @Test
    public void test() {
        TreeNode root = new TreeNode(1);
        TreeNode left = new TreeNode(2);
        TreeNode right = new TreeNode(3);
        root.left = left;
        root.right = right;
        System.out.println(sumNumbers(root));
    }

    public int sumNumbers(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return traversalTree(root, 0, 0);
    }

    private int traversalTree(TreeNode node, int sum, int tmp) {

        tmp = node.val + tmp * 10;
        if (node.left == null && node.right == null) {
            return sum + tmp;
        }
        if (node.left != null) {
            sum = traversalTree(node.left, sum, tmp);
        }
        if (node.right != null) {
            sum = traversalTree(node.right, sum, tmp);
        }
        return sum;
    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}